//
//  MCSinglePickerView.swift
//  StoreManage
//
//  Created by MC on 2018/2/1.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

protocol MCYearAndMonthPickerViewDelegate : NSObjectProtocol {
    func MCYearAndMonthPickerViewSelectedRow(str:String)
}

class MCYearAndMonthPickerView: UIView {
    
    
    weak var delegate : MCYearAndMonthPickerViewDelegate?
    
    var monthArray : NSArray = NSArray()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.init(white: 0, alpha: 0.4)
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(dismissSelf))
        self.addGestureRecognizer(tap)
        
        self.addSubview(bgView)
        bgView.snp.remakeConstraints { (make) ->Void in
            make.left.bottom.right.equalTo(self)
            make.height.equalTo(200)
        }
        
        bgView.addSubview(cancelButton)
        cancelButton.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(20)
            make.top.equalTo(10)
            make.size.equalTo(CGSize.init(width: 60, height: 25))
        }
        
        bgView.addSubview(sureButton)
        sureButton.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(-20)
            make.top.equalTo(10)
            make.size.equalTo(CGSize.init(width: 60, height: 25))
        }
        
        bgView.addSubview(showLabel)
        showLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(cancelButton.snp.right).offset(10)
            make.right.equalTo(sureButton.snp.left).offset(-10)
            make.centerY.equalTo(cancelButton)
        }
        
        bgView.addSubview(pickerView)
        pickerView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(10)
            make.right.equalTo(-10)
            make.top.equalTo(sureButton.snp.bottom).offset(10)
            make.bottom.equalTo(-10)
        }
        
        createData()
    }
    
    func createData() {
        let formmater = DateFormatter.init()
        formmater.dateFormat = "yyyy-MM-dd"
        let dateStr = formmater.string(from: Date.init())
        let array = dateStr.components(separatedBy: "-")
        let currentYear = array[0]
        
        yearsArrayM = NSMutableArray()
        
        for i in 0..<5 {
            let year = Int(currentYear)
            yearsArrayM.add(String(year! - i))
        }
        
        monthArray = ["01","02","03","04","05","06","07","08","09","10","11","12"]
        
        pickerView.reloadAllComponents()
    }
    
    @objc func dismissSelf() {
        self.removeFromSuperview()
    }
    
    @objc func emptyEvent() {}
    
    @objc func sureButtonClicked() {
        self.removeFromSuperview()
        let yearRow = pickerView.selectedRow(inComponent: 0)
        let monthRow = pickerView.selectedRow(inComponent: 1)
        
        let year = yearsArrayM[yearRow] as? String ?? ""
        let month = monthArray[monthRow] as? String ?? ""
        
        let str = year + "-" + month
        
        delegate?.MCYearAndMonthPickerViewSelectedRow(str: str)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var bgView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(emptyEvent))
        view.addGestureRecognizer(tap)
        
        return view
    }()
    
    lazy var cancelButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 3
        button.layer.borderColor = UIColor.MCMain.cgColor
        button.layer.borderWidth = 1
        button.titleLabel?.font = MCFont(13)
        button.setTitle("取消", for: UIControlState.normal)
        button.setTitleColor(UIColor.MCMain, for: UIControlState.normal)
        button.addTarget(self, action: #selector(dismissSelf), for: UIControlEvents.touchUpInside)
        return button
    }()
    
    lazy var sureButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 3
        button.backgroundColor = UIColor.MCMain
        button.titleLabel?.font = MCFont(13)
        button.setTitle("确定", for: UIControlState.normal)
        button.setTitleColor(UIColor.white, for: UIControlState.normal)
        button.addTarget(self, action: #selector(sureButtonClicked), for: UIControlEvents.touchUpInside)
        return button
    }()
    
    lazy var showLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(16)
        label.textColor = UIColor.black
        label.textAlignment = NSTextAlignment.center
        return label
    }()
    
    lazy var pickerView: UIPickerView = {
        let view = UIPickerView()
        view.delegate = self
        view.dataSource = self
        view.selectedRow(inComponent: 0)
        view.showsSelectionIndicator = true
        view.backgroundColor = UIColor.groupTableViewBackground
        return view
    }()
    
    lazy var yearsArrayM: NSMutableArray = {
        let arrayM = NSMutableArray.init()
        return arrayM
    }()
}


extension MCYearAndMonthPickerView : UIPickerViewDelegate,UIPickerViewDataSource {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 2
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        switch component {
        case 0:
            return yearsArrayM.count
        default:
            return monthArray.count
        }
    }
    
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 50
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        switch component {
        case 0:
            let year = yearsArrayM[row] as? String ?? ""
            
            return year + "年"
        default:
            let month = monthArray[row] as? String ?? ""
            return month + "月"
        }
    }
}


